import {Component} from "@angular/core";
import {Events, IonicPage, NavController, NavParams, ToastController} from "ionic-angular";
import {RosterFliterProvider} from "../../../providers/activity/roster-filter/roster-fliter.provider";
import {
    ActivityClassify, ActivityFilterModel,
    ActivityStatus
} from "../../../model/pages/activity/activity-filter/activity-filter.model";
import {ActivityListItemModel} from "../../../model/pages/activity/activity-list/activity-list-item.model";
import {AddStaffTrack, TrackStatus} from "../../../model/pages/activity/roster-list/roster-list-item.model";
import {RosterFilterModel} from "../../../model/pages/activity/roster-filter/roster-filter.model";
import {ActivityListProvider} from "../../../providers/activity/activity-list/activity-list.provider";
import {DateService} from '../../../services/common/date.service';

@IonicPage()
@Component({
    selector: 'roster-filter',
    templateUrl: 'roster-filter.html',
    providers: [RosterFliterProvider, ActivityListProvider,DateService]
})
export class RosterFilterPage {

    private pickerOption = {
        cssClass: 'roster-filter_picker'
    };
    //筛选条件对象
    private rosterFilteModel: RosterFilterModel = new RosterFilterModel();
    //筛选条件Model
    private activityFil: ActivityFilterModel = new ActivityFilterModel();
    //活动类型列表
    private typeList = [];
    //活动列表
    private activityList: ActivityListItemModel[] = [];
    //是否禁用查询按钮
    private disabled: boolean = false;
    //活动状态列表
    private statusList = [{
        id: ActivityStatus.notStart,
        name: '未开始'
    }, {
        id: ActivityStatus.processing,
        name: '进行中'
    }, {
        id: ActivityStatus.over,
        name: '已结束'
    }];
    //跟进状态
    private followList = [{
        id: TrackStatus.notFollow,
        name: '未跟进'
    }, {
        id: TrackStatus.contacted,
        name: '已拨打'
    }, {
        id: TrackStatus.notContacted,
        name: '未接通'
    }, {
        id: TrackStatus.notInterested,
        name: '客户不感兴趣'
    }, {
        id: TrackStatus.waitCallback,
        name: '待回电'
    }, {
        id: TrackStatus.overInterview,
        name: '已面谈'
    }, {
        id: TrackStatus.overPrint,
        name: '已打印计划书'
    }, {
        id: TrackStatus.complete,
        name: '已完成'
    }];

    private followList2 = [{
        id: AddStaffTrack.notFollow,
        name: '未跟进'
    },{
        id: AddStaffTrack.process,
        name: '跟进中'
    },{
        id: AddStaffTrack.notIntend,
        name: '暂不跟进'
    }];

    private isExpandName = true;
    // 年度列表
    private yearList:any[]=[];

    private canSubmit = true;

    constructor(private events: Events,
                private rosterPro: RosterFliterProvider,
                private navCtrl: NavController,
                private navParams: NavParams,
                private activityListPro: ActivityListProvider,
                public dateService:DateService,
                public toastCtrl: ToastController
                ) {
        this.yearList=this.dateService.getYearList(2010);
    }

    ionViewDidLoad() {
        this.rosterFilteModel = this.navParams.get('tempFil');
        if (this.rosterFilteModel.classify === ActivityClassify.marketActivity) {
            this.getAllActivityType();
        }
    }

    getAllActivity() {
        this.activityListPro.getActivityList(this.activityFil, 0)
            .subscribe((res: ActivityListItemModel[]) => {
                this.activityList = res;
            });
    }

    getAllActivityType() {
        this.activityListPro.getActivityType(this.rosterFilteModel.classify)
            .subscribe((res: Array<any>) => {
                res.forEach((type) => {
                    this.activityFil.type.push(type.id);
                });
                this.typeList = res;
                this.getAllActivity();
            });
    }

    changeFilter(key, val) {
        let index = this.rosterFilteModel[key].indexOf(val);
        if (index === -1) {
            this.rosterFilteModel[key].push(val);
        } else {
            this.rosterFilteModel[key].splice(index, 1);
        }
    }

    isSelectBtn(key, val) {
        return !(this.rosterFilteModel[key].indexOf(val) === -1);
    }

    cleanTime() {
        this.rosterFilteModel.starttime = this.rosterFilteModel.endtime = null;
    }

    cleanTakeTime () {
        this.rosterFilteModel.endTakeTime = this.rosterFilteModel.startTakeTime = null;
    }

    resetForm() {
        this.rosterFilteModel.trackStatus = [];
        this.rosterFilteModel.startTakeTime=null;
        this.rosterFilteModel.endTakeTime=null;
        this.rosterFilteModel.endtime = null;
        this.rosterFilteModel.starttime = null;
        this.rosterFilteModel.ids = [];
        this.rosterFilteModel.status = [];
        this.rosterFilteModel.type = [];
    }

    doSearch() {
        this.events.publish('roster:search', this.rosterFilteModel);
        this.navCtrl.pop();
    }

    validateForm () {
        if (this.rosterFilteModel.starttime && this.rosterFilteModel.endtime
            && new Date(this.rosterFilteModel.starttime) > new Date(this.rosterFilteModel.endtime)) {
            this.toastCtrl.create({
                duration: 1500,
                position: 'middle',
                message: '活动开始时间输入有误'
            }).present();
            this.canSubmit = false;
        } else {
            this.canSubmit = true;
        }
    }

    validateForm2 () {
        if (this.rosterFilteModel.startTakeTime && this.rosterFilteModel.endTakeTime
            && new Date(this.rosterFilteModel.startTakeTime) > new Date(this.rosterFilteModel.endTakeTime)) {
            this.toastCtrl.create({
                duration: 1500,
                position: 'middle',
                message: '活动参与时间输入有误'
            }).present();
            this.canSubmit = false;
        } else {
            this.canSubmit = true;
        }
    }
}
